#!/bin/bash

set -e -u

# configure locales
ln -sf /usr/share/zoneinfo/UTC /etc/localtime
sed -i 's/#\(en_US\.UTF-8\)/\1/' /etc/locale.gen
sed -i 's/#\(es_ES\.UTF-8\)/\1/' /etc/locale.gen
sed -i 's/#\(gl_ES\.UTF-8\)/\1/' /etc/locale.gen
sed -i 's/#\(pt_BR\.UTF-8\)/\1/' /etc/locale.gen
locale-gen

# configure sudo
chmod 750 /etc/sudoers.d
chmod 440 /etc/sudoers.d/g_wheel

# configure pacman
sed -i "s/#Server/Server/g" /etc/pacman.d/mirrorlist

# configure services
systemctl enable multi-user.target      \
                 pacman-init.service    \
                 choose-mirror.service  \
                 NetworkManager.service

# configure root shell
usermod -s /bin/bash root 2> /dev/null

# configure live user and login
LIVE_USER=parabola
LIVE_USER_GROUPS="adm,audio,floppy,log,network,rfkill,scanner,storage,optical,power,wheel"
cp /usr/share/applications/calamares.desktop /etc/skel/Desktop/
[ -z "$(id ${LIVE_USER} 2> /dev/null)" ] && useradd -m -p "" -g users -G "$LIVE_USER_GROUPS" -s /bin/bash ${LIVE_USER}
sed -i "s|--autologin root|--autologin ${LIVE_USER}|" /etc/systemd/system/getty@tty1.service.d/autologin.conf
# sed -i 's/^exec -l \$SHELL -c "$CK_XINIT_SESSION \\"$LXSESSION\\""/ch-launch-session "$LXSESSION"/' /etc/lxdm/Xsession
sed -i "s|_EDITION_TITLE_|${iso_title}|" /etc/motd
# echo "@/home/${LIVE_USER}/.language.sh" >> /home/${LIVE_USER}/.config/lxsession/LXDE/autostart

# TODO: debug - delete these
sed -i 's|^Icon=.*$|Icon=/root/\.scriptsInstallation/parabolaicon\.png|' /etc/skel/Desktop/calamares.desktop
sed -i 's|^Terminal=.*$|Terminal=true|' /etc/skel/Desktop/calamares.desktop
echo 'X-KeepTerminal=True' >> /etc/skel/Desktop/calamares.desktop
